<template>
  <div class="department-members">
    <div class="header">
      <h2>部门与角色管理 / 成员名单</h2>
      <div class="search-bar">
        <select v-model="statusFilter">
          <option value="">请选择人员状态</option>
          <option value="激活">激活</option>
          <option value="冻结">冻结</option>
        </select>
        <select v-model="roleFilter">
          <option value="">请选择系统角色</option>
          <option value="教职工">教职工</option>
          <option value="考务科科长">考务科科长</option>
          <option value="学院研工办主任">学院研工办主任</option>
          <option value="综合办主任">综合办主任</option>
          <option value="副院长">副院长</option>
        </select>
        <input
            type="text"
            placeholder="请输入姓名/工号关键词查询"
            v-model="keyword"
        >
        <button @click="searchMembers">查询</button>
        <button @click="resetFilters">重置</button>
        <button>添加成员</button>
        <button>批量导入成员</button>
      </div>
    </div>

    <table class="member-table">
      <thead>
      <tr>
        <th>序号</th>
        <th>姓名</th>
        <th>工号</th>
        <th>系统角色</th>
        <th>角色代码</th>
        <th>人员状态</th>
        <th>添加时间</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr v-for="(member, index) in filteredMembers" :key="member.id">
        <td>{{ index + 1 }}</td>
        <td>{{ member.name }}</td>
        <td>{{ member.employeeId }}</td>
        <td>{{ member.role }}</td>
        <td>{{ member.roleCode }}</td>
        <td>{{ member.status }}</td>
        <td>{{ member.addTime }}</td>
        <td class="actions">
          <div class="dropdown">
            <button class="dropdown-toggle">变更</button>
            <div class="dropdown-menu">
              <a @click="activateMember(member)">激活账户</a>
              <a @click="freezeMember(member)">冻结账户</a>
              <a @click="removeMember(member)">移除成员</a>
            </div>
          </div>
        </td>
      </tr>
      </tbody>
    </table>

    <div class="footer-notes">

      <button class="batch-update">批量变更</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'

interface Member {
  id: number
  name: string
  employeeId: string
  role: string
  roleCode: string
  status: string
  addTime: string
  departmentCode?: string
}

const route = useRoute()
const statusFilter = ref('')
const roleFilter = ref('')
const keyword = ref('')

// 模拟成员数据
const members: Member[] = [
  { id: 1, name: 'QIANJIAXIN1', employeeId: '10001', role: '教职工', roleCode: 'role1', status: '激活', addTime: '2023/10/17 16:57:35' },
  { id: 2, name: 'QIANJIAXIN2', employeeId: '10002', role: '教职工', roleCode: 'role1', status: '激活', addTime: '2023/10/17 16:57:35' },
  { id: 3, name: 'QIANJIAXIN3', employeeId: '10003', role: '教职工', roleCode: 'role1', status: '激活', addTime: '2023/10/17 16:57:35' },
  { id: 4, name: 'QIANJIAXIN4', employeeId: '10004', role: '教职工', roleCode: 'role1', status: '激活', addTime: '2023/10/17 16:57:35' },
  { id: 5, name: 'QIANJIAXIN5', employeeId: '10005', role: '教职工', roleCode: 'role1', status: '激活', addTime: '2023/10/17 16:57:35' }
]

const departmentCode = ref('')

onMounted(() => {
  if (route.query.departmentCode) {
    departmentCode.value = route.query.departmentCode as string
    // 这里可以添加根据部门代码过滤成员的逻辑
    console.log('加载部门成员:', departmentCode.value)
  }
})

const filteredMembers = computed(() => {
  let result = [...members]

  if (statusFilter.value) {
    result = result.filter(m => m.status === statusFilter.value)
  }

  if (roleFilter.value) {
    result = result.filter(m => m.role === roleFilter.value)
  }

  if (keyword.value) {
    const kw = keyword.value.toLowerCase()
    result = result.filter(m =>
        m.name.toLowerCase().includes(kw) ||
        m.employeeId.toLowerCase().includes(kw)
    )
  }

  return result
})

function searchMembers() {
  // 搜索逻辑已在计算属性中处理
}

function resetFilters() {
  statusFilter.value = ''
  roleFilter.value = ''
  keyword.value = ''
}

function activateMember(member: Member) {
  console.log('激活成员:', member.name)
  member.status = '激活'
}

function freezeMember(member: Member) {
  console.log('冻结成员:', member.name)
  member.status = '冻结'
}

function removeMember(member: Member) {
  console.log('移除成员:', member.name)
  const index = members.findIndex(m => m.id === member.id)
  if (index !== -1) {
    members.splice(index, 1)
  }
}

function confirmActivateMember(member: Member) {
  if (confirm(`是否确认激活账户：${member.name}`)) {
    activateMember(member)
  }
}

function confirmFreezeMember(member: Member) {
  if (confirm(`是否确认冻结账户：${member.name}`)) {
    freezeMember(member)
  }
}

function confirmRemoveMember(member: Member) {
  if (confirm(`是否确认移除成员：${member.name}`)) {
    removeMember(member)
  }
}

</script>

<style scoped>
.department-members {
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.header {
  margin-bottom: 20px;
}

.search-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 15px;
}

.search-bar select,
.search-bar input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.search-bar input {
  width: 200px;
}

button {
  padding: 8px 16px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

button:hover {
  background-color: #2980b9;
}

.member-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.member-table th,
.member-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.member-table th {
  background-color: #f8f9fa;
  font-weight: 600;
}

.actions {
  display: flex;
  justify-content: center;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-toggle {
  background-color: #6c757d;
}

.dropdown-menu {
  display: none;
  position: absolute;
  background-color: white;
  min-width: 120px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  border-radius: 4px;
  z-index: 100;
  overflow: hidden;
}

.dropdown:hover .dropdown-menu {
  display: block;
}

.dropdown-menu a {
  display: block;
  padding: 8px 16px;
  text-decoration: none;
  color: #333;
  cursor: pointer;
  transition: background-color 0.2s;
}

.dropdown-menu a:hover {
  background-color: #f8f9fa;
}

.footer-notes {
  margin-top: 30px;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.6;
}

.footer-notes p {
  margin-bottom: 8px;
}

.batch-update {
  margin-top: 15px;
  background-color: #27ae60;
}

.batch-update:hover {
  background-color: #219653;
}
</style>